# coding=GBK
'''
Created on 2011-1-6

@author: Administrator
'''
from util import DB
import uuid
import config
def loadUrlCat(path="../" + config.data_dir + config.url_type): 
    f = open(path, 'r')
    cur = DB.getCur()
    cur.execute("truncate %s" % config.url_cat_tbname)
    cur.execute("set names %s" % config.db['charset'])
    for line in f.readlines():
        t = line.split(',')
        urlid = uuid.uuid3(uuid.NAMESPACE_URL, t[0]).hex
        sql = "insert into %s values('%s','%s','%s','%s','%s','%s',null)" % (config.url_cat_tbname, urlid, t[0], t[1], t[2], t[3], t[4])
        print sql
        try:
            cur.execute(sql)
        except (Exception), e:
            if str(e).startswith("_mysql_exceptions.IntegrityError"):
                continue
        
    cur.close()
    f.close()
        
def loadUserInfo():
#    cur = DB.getCur()
#    cur.execute("drop table if exists %s" % config.ms_sessuser_tbname)
#    cur.execute("set names %s" % config.db['charset'])
##    cur.execute("insert into userinfo \
##                select s.sess_key, s.ip, i.loc, i.loc2   \
##                from (select distinct ip, sess_key from simplesession)as s, ip2geo0  as i \
##                where s.ip > i.startip and s.ip < i.endip;")
    cur1 = DB.getCur()
    cur2 = DB.getCur()
    cur2.execute("truncate %s" % config.ms_sessuser_tbname)
    cur1.execute("set names %s" % config.db['charset'])
    cur2.execute("set names %s" % config.db['charset'])
    count = cur1.execute("select s.sess_key, s.ip, i.loc, i.loc2   \
                from (select distinct ip, sess_key from simplesession)as s, ip2geo0  as i \
                where s.ip > i.startip and s.ip < i.endip;")
    for i in range(count):
        t = cur1.fetchone()
        sql = "insert into %s values('%s',%d,'%s','%s')"%(config.ms_sessuser_tbname,t[0],t[1],t[2],t[3])
        cur2.execute(sql)
        
def loadClickPath():
    cur1 = DB.getCur()
    cur2 = DB.getCur()
    cur1.execute("set names %s" % config.db['charset'])
    cur2.execute("set names %s" % config.db['charset'])
    cur2.execute("truncate %s" % config.ms_clickpath_tbname)
    count = cur1.execute("select  s.sess_key,s.url,s.urlid,pagetype,cat1,cat2,cat3 from simplesession s,urlcat u where s.urlid = u.urlid order by s.sess_key,s.time")
    
    seqid = 1
    sesskey = ""
    count-=1
    for i in range(count):
        t = cur1.fetchone()
        if t[0]!=sesskey:
            seqid = 1
            sesskey = t[0]
        else:
            seqid += 1
        sql = "insert into %s values('%s','%s','%s','%s','%s','%s','%s',%d)" % (config.ms_clickpath_tbname,sesskey, t[1], t[2], t[3], t[4], t[5], t[6], seqid)
        print sql
        cur2.execute(sql)
    
    cur1.close()
    cur2.close()
    
if __name__ == '__main__':
    import time
    t1 = time.time()
#    loadUrlCat()
#    loadUserInfo()
    loadClickPath()
    print time.time() -t1
    print "loaded"
